/**
 * Created by linxh on 2015/9/24.
 */
define("framework/snapshot/GillionLocationServiceConstructor",['angular'],function(angular){



    return function(ZIndex){

        function Location(){

        }

        Location.prototype.createHtmltoTop = function(outHtml,thisWindow){
            if(!thisWindow) thisWindow = window;
            var outJqlite = angular.element(outHtml),
                me = this;
            outJqlite.css("z-index",ZIndex.getMaxZIndex());
            angular.element(thisWindow).on("unload",function(){
                outJqlite.remove();
            });
            angular.element(thisWindow).on("scroll",function(){
                outJqlite.css("display","none");
            });
            if(!me.topWindow){
                me.topWindow = me.getTopWindow(thisWindow);
            }
            if(me.topWindow != thisWindow){
                angular.element(me.topWindow).on("scroll",function(){
                    outJqlite.css("display","none");
                });
            }
            me.topWindow.document.body.appendChild(outJqlite[0]);
            return outJqlite;
        }

        /**
         * 往某个窗口添加文件
         * @param myWindow 窗口dom
         * @param filePath 文件路径
         */
        Location.prototype.addCssFile = function(myWindow,filePath){
            var head = myWindow.document.getElementsByTagName('head')[0];
            var link = document.createElement('link');
            link.href = filePath;
            link.rel = 'stylesheet';
            link.type = 'text/css';
            head.appendChild(link);
        }

        /**
         * @param element 对应元素
         * @param thisWindow 窗口dom
         * @param topWindow 窗口dom
         */
        Location.prototype.calculateLocation = function(element,thisWindow,topWindow){
            if(!thisWindow) thisWindow = window;
            if(!topWindow){
                topWindow = this.getTopWindow(thisWindow);
            }
            element = angular.element(element);
            element = element[0];
            var currentWindow,
                scrollLeft = thisWindow.document.body.scrollLeft ,
                scrollTop =  thisWindow.document.body.scrollTop ,
                position = {"left":this.getRealOffsetLeft(element)-this.getScrollLeft(element),
                    "top":this.getRealOffsetTop(element)-this.getScrollTop(element)};

            for(currentWindow = thisWindow;currentWindow!=topWindow;currentWindow=currentWindow.parent){
                var parentIframe = this.getParentIframe(currentWindow);
                position.left += this.getRealOffsetLeft(parentIframe)-scrollLeft-this.getScrollLeft(parentIframe);
                position.top += this.getRealOffsetTop(parentIframe)-scrollTop-this.getScrollTop(parentIframe);
                //因为弹出框position是fix属性 导致getRealOffsetLeft是获取无关滚动条的长度
                if(currentWindow.name.indexOf("msgBox")!=-1){
                    position.left += currentWindow.parent.document.body.scrollLeft;
                    position.top += currentWindow.parent.document.body.scrollTop;
                }

                scrollLeft = currentWindow.document.body.scrollLeft;
                scrollTop = currentWindow.document.body.scrollTop;
            }
            var docElement = topWindow.document.documentElement,
                docWidth = docElement.clientWidth + topWindow.document.body.scrollLeft;
                docHeight = docElement.clientHeight + topWindow.document.body.scrollTop;

            position.right = docWidth - position.left ;
            position.bottom = docHeight - position.top ;
            return position;
        }

        /**
         * @param
         *
         */
        Location.prototype.calculateDirection = function(target,next,winBody,directionFirst,position){
            if(!winBody){
                winBody = this.getTopWindow(window).document.body;
            }
            //方向优先 默认底部优先
            var offsetLeft = 0;this.getRealOffsetLeft(target);
            var offsetTop = 0;this.getRealOffsetTop(target);
            if(position){
                offsetLeft = position.left;
                offsetTop = position.top;
            }else{
                offsetLeft = this.getRealOffsetLeft(target);
                offsetTop = this.getRealOffsetTop(target);
            }
            if(directionFirst=="right"){
                if(next.offsetWidth+target.offsetWidth + offsetLeft + 30 < winBody.clientWidth){
                    return {left:target.offsetWidth,top:0,direction:"right"};
                }
            }
            if(next.offsetHeight + target.offsetHeight + offsetTop < winBody.clientHeight){
                return {left:0,top:target.offsetHeight,direction:"bottom"};
            }else if(next.offsetHeight < offsetTop){
                return {left:0,top:-(next.offsetHeight),direction:"top"};
            }else  if(next.offsetWidth+target.offsetWidth + offsetLeft + 30 < winBody.clientWidth){
                return {left:target.offsetWidth,top:0,direction:"right"};
            }else{
                return {left:0,top:target.offsetHeight,direction:"bottom"};
            }
        }

        /**
         *
         * @param obj dom对象
         */
        Location.prototype.getRealOffsetLeft = function(obj){
            var left=obj.offsetLeft;
            while (obj = obj.offsetParent) {
                left += obj.offsetLeft; //叠加父容器的左边距
            }
            return left;
        }

        /**
         *
         * @param obj dom对象
         */
        Location.prototype.getScrollTop = function(obj){
            var scrollTop = 0;
            obj = obj.parentNode;
            while (obj && obj.tagName != 'BODY') {
                scrollTop += obj.scrollTop; //叠加父容器的上滚动边距
                obj = obj.parentNode;
            }
            return scrollTop;
        }

        /**
         *
         * @param obj dom对象
         */
        Location.prototype.getScrollLeft = function(obj){
            var scrollLeft = 0;
            obj = obj.parentNode;
            while (obj && obj.tagName != 'BODY') {
                scrollLeft += obj.scrollLeft; //叠加父容器的左边距
                obj = obj.parentNode;
            }
            return scrollLeft;
        }

        /**
         *
         * @param obj dom对象
         */
        Location.prototype.getRealOffsetTop = function(obj){
            var top=obj.offsetTop;
            while (obj = obj.offsetParent) {
                top += obj.offsetTop; //叠加父容器的左边距
            }
            return top;
        }

        Location.prototype.getParentIframe = function(thisWindow){
            var a = thisWindow.parent.document.getElementsByTagName("iframe");
            for(var i=0;i<a.length;i++){
                if(a[i].contentWindow==thisWindow){
                    return a[i];
                }
            }
            return null;
        }

        /**
         * 获取框架的top
         * @param win
         * @returns {*}
         */
        Location.prototype.getTopWindow = function (win) {
            var href = win.location.href,
                mainWin = win;
            if (href.indexOf('__showUrl=true') > -1) {
                mainWin =this.getTopWindow(mainWin.parent);
            }
            return mainWin;
        }

        return new Location();
    }
})
